import 'package:get/get.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../../../config/router_page_name.dart';
import '../../../models/banner.dart';
import '../../../services/banner_service.dart';

class ManageBannersLogic extends GetxController {
  final BannerService _bannerService = BannerService();
  
  final isLoading = true.obs;
  final RxList<BannerItem> bannerList = <BannerItem>[].obs;
  
  @override
  void onInit() {
    super.onInit();
    loadBanners();
  }
  
  Future<void> loadBanners() async {
    isLoading.value = true;
    try {
      final banners = await _bannerService.getAllBanners();
      bannerList.value = banners;
    } catch (e) {
      SmartDialog.showToast('加载轮播图失败: $e');
    } finally {
      isLoading.value = false;
    }
  }
  
  void goToAddBanner() {
    Get.toNamed('/add-banner')?.then((_) => loadBanners());
  }
  
  Future<void> deleteBanner(int id) async {
    try {
      await _bannerService.deleteBanner(id);
      SmartDialog.showToast('删除成功');
      await loadBanners();
    } catch (e) {
      SmartDialog.showToast('删除失败: $e');
    }
  }
}
